
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

//~--- non-JDK imports --------------------------------------------------------
import beans.DisplayTopicActionFormBean;
import dto.JfMembers;

import dto.Topics;
import dto.TopicsHome;

import moderator.ModeratorControlPanel;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

//~--- JDK imports ------------------------------------------------------------


import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author Deena
 */
public class TestTopicsAction extends org.apache.struts.actions.DispatchAction {

    Vector<Topics> selectedTopics;
    TopicsHome topicHome = new TopicsHome();
    moderator.ModeratorControlPanel modPanel = new ModeratorControlPanel();
    Topics topic;
    String[] topicIDs;

    public ActionForward Delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
        DisplayTopicActionFormBean topicsInt = (DisplayTopicActionFormBean) form;
        ActionForward forward;
        selectedTopics = new Vector<Topics>();

        for (int i = 0; i < topicsInt.getNumber().length; i++) {
            topic = new Topics(new Integer(topicsInt.getNumber()[i]));
            selectedTopics.add(topic);
        }
        JfMembers mod = (JfMembers) request.getSession(false).getAttribute("userBean");
        Integer secId = topicsInt.getSectionId();
        boolean prevelge = modPanel.checkprevelage(secId, mod);
        if (prevelge) {
            modPanel.delete(selectedTopics);
            forward = new ActionForward("/DisplayTopicAction.do?section=" + topicsInt.getSectionId());
        } else {
            System.out.println("error no prevlege");
            ActionErrors error = new ActionErrors();
            error.add("PrevError", new ActionMessage("Prev"));
            saveErrors(request, error);
            forward = new ActionForward("/DisplayTopicAction.do?section=" + topicsInt.getSectionId());
        }
        return forward;
    }

    public ActionForward SetClose(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
        ActionForward forward;
        DisplayTopicActionFormBean topicsInt = (DisplayTopicActionFormBean) form;
        selectedTopics = new Vector<Topics>();
        for (int i = 0; i < topicsInt.getNumber().length; i++) {
            topic = new Topics(new Integer(topicsInt.getNumber()[i]));
            selectedTopics.add(topic);
        }

        JfMembers mod = (JfMembers) request.getSession(false).getAttribute("userBean");
        Integer secId = topicsInt.getSectionId();

        boolean prevelge = modPanel.checkprevelage(secId, mod);
        if (prevelge) {
            modPanel.setClose(selectedTopics, true);
            forward = new ActionForward("/DisplayTopicAction.do?section=" + topicsInt.getSectionId());
        } else {
            System.out.println("error no prevlege");
            ActionErrors error = new ActionErrors();
            error.add("PrevError", new ActionMessage("Prev"));
            saveErrors(request, error);
            forward = new ActionForward("/DisplayTopicAction.do?section=" + topicsInt.getSectionId());
        }
        return forward;
    }

    public ActionForward SetSticky(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
        ActionForward forward;
        DisplayTopicActionFormBean topicsInt = (DisplayTopicActionFormBean) form;

        selectedTopics = new Vector<Topics>();
        for (int i = 0; i < topicsInt.getNumber().length; i++) {
            topic = new Topics(new Integer(topicsInt.getNumber()[i]));
            selectedTopics.add(topic);
        }


        JfMembers mod = (JfMembers) request.getSession(false).getAttribute("userBean");
        Integer secId = topicsInt.getSectionId();

        boolean prevelge = modPanel.checkprevelage(secId, mod);
        if (prevelge) {
            modPanel.setSticking(selectedTopics, true);
            forward = new ActionForward("/DisplayTopicAction.do?section=" + topicsInt.getSectionId());
        } else {
            System.out.println("error no prevlege");
            ActionErrors error = new ActionErrors();
            error.add("PrevError", new ActionMessage("Prev"));
            saveErrors(request, error);
            forward = new ActionForward("/DisplayTopicAction.do?section=" + topicsInt.getSectionId());
        }
        return forward;
    }

    public ActionForward Merge(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {

        ActionForward forward;
        DisplayTopicActionFormBean topics = (DisplayTopicActionFormBean) form;

        selectedTopics = new Vector<Topics>();
        Vector<Integer> ids = new Vector<Integer>();

        for (int i : topics.getNumber()) {
            System.out.println("" + i);
            ids.add(new Integer(i));
        }


        JfMembers mod = (JfMembers) request.getSession(false).getAttribute("userBean");
        Integer secId = topics.getSectionId();

        boolean prevelge = modPanel.checkprevelage(secId, mod);
        if (prevelge) {
            modPanel.merge(ids, topics.getName());
            forward = new ActionForward("/DisplayTopicAction.do?section=" + topics.getSectionId());
        } else {
            System.out.println("error no prevlege");
            ActionErrors error = new ActionErrors();
            error.add("PrevError", new ActionMessage("Prev"));
            saveErrors(request, error);
            forward = new ActionForward("/DisplayTopicAction.do?section=" + topics.getSectionId());
        }
        return forward;
    }

    public ActionForward Rename(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {

        ActionForward forward;
        System.out.print("in rename");
        DisplayTopicActionFormBean topicsInt = (DisplayTopicActionFormBean) form;
        selectedTopics = new Vector<Topics>();
        for (int c : topicsInt.getNumber()) {
            System.out.println("" + c);
        }
        for (int i = 0; i < topicsInt.getNumber().length; i++) {
            topic = new Topics(new Integer(topicsInt.getNumber()[i]));
            selectedTopics.add(topic);
        }
        //String rename = topicsInt.getRename();
        String rename = topicsInt.getName();


        JfMembers mod = (JfMembers) request.getSession(false).getAttribute("userBean");
        Integer secId = topicsInt.getSectionId();

        boolean prevelge = modPanel.checkprevelage(secId, mod);
        if (prevelge) {
            modPanel.rename(topic, rename);
            forward = new ActionForward("/DisplayTopicAction.do?section=" + topicsInt.getSectionId());
        } else {
            System.out.println("error no prevlege");
            ActionErrors error = new ActionErrors();
            error.add("PrevError", new ActionMessage("Prev"));
            saveErrors(request, error);
            forward = new ActionForward("/DisplayTopicAction.do?section=" + topicsInt.getSectionId());
        }
        return forward;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com

